<!DOCTYPE html>
<html lang=en>
<head>
    <meta charset=utf-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <script src="https://cdn.jsdelivr.net/gh/eduardolundgren/tracking.js/build/tracking-min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/eduardolundgren/tracking.js/build/data/face-min.js"></script>
    <script type="text/javascript" src="{__SGHOME_PATH}js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="{__SGHOME_PATH}js/vconsole.min.js"></script>
    <script>
        var deviceWidth = document.documentElement.clientWidth;
        document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
        var vConsole = new window.VConsole();
    </script>
    <style>
        video::-webkit-media-controls-start-playback-button {
            display: none;
        }
    </style>
</head>
<body>
<video id="video" style="display: none;" width="480" height="320"
       muted
       x5-playsinline="true"
       playsinline="true"
       webkit-playsinline="true"
       x-webkit-airplay="true"
       x5-video-orientation="portraint" >
</video>
<div>
    <button id="capture">拍照</button>
</div>
<canvas id="canvas" width="480" height="320"></canvas>

</body>

<script type="text/javascript">
    //访问用户媒体设备的兼容方法
    function getUserMedia(constraints, success, error) {
        if (navigator.mediaDevices.getUserMedia) {
            //最新的标准API
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
        } else if (navigator.webkitGetUserMedia) {
            //webkit核心浏览器
            navigator.webkitGetUserMedia(constraints,success, error)
        } else if (navigator.mozGetUserMedia) {
            //firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error);
        } else if (navigator.getUserMedia) {
            //旧版API
            navigator.getUserMedia(constraints, success, error);
        }
    }

    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let context = canvas.getContext('2d');

    function success(stream) {
        //兼容webkit核心浏览器
        let CompatibleURL = window.URL || window.webkitURL;
        //将视频流设置为video元素的源
        console.log(stream);

        // video.src = CompatibleURL.createObjectURL(stream);
        video.srcObject = stream;
        video.play();
        video.style.display = 'block';
    }

    function error(error) {
        video.style.display = 'none';
        console.log(`访问用户媒体设备失败${error.name}, ${error.message}`);
    }

    if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
        //调用用户媒体设备, 访问摄像头
        getUserMedia({video : {width: 480, height: 320, facingMode: "user"}}, success, error);
    } else {
        alert('不支持访问用户媒体');
    }

    document.getElementById('capture').addEventListener('click', function () {
        context.drawImage(video, 0, 0, 480, 320);
    })
</script>

</html>